#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newlock.h"
#include "enterpassword.h"

unsigned char *QString2Key(QString str)
{
    QByteArray ba = str.toLocal8Bit();
    char *ptr = ba.data();
    char *p = ba.data();
    int len = 0;
    while (*p != '\0')
    {
        len++;
        p++;
    }
    unsigned char *result = (unsigned char *)malloc(16);
    char *tmp = (char *)malloc(len);
    memset(result, 0, 16);
    memset(tmp, 0, len);
    memcpy(tmp, ptr, len);
    int n = len > 16 ? 16 : len;
    for (int i = 0; i < n; i++)
    {
        result[i] = (unsigned char)tmp[i];
    }
    free(tmp);
    return result;
}

unsigned char *QString2Input(QString str)
{
    QByteArray ba = str.toLocal8Bit();
    char *ptr = ba.data();
    char *p = ba.data();
    int len = 0;
    while (*p != '\0')
    {
        len++;
        p++;
    }
    unsigned char *result = (unsigned char *)malloc(len);
    char *tmp = (char *)malloc(len);
    memset(result, 0, 16);
    memset(tmp, 0, len);
    memcpy(tmp, ptr, len);
    for (int i = 0; i < len; i++)
    {
        result[i] = (unsigned char)tmp[i];
    }
    free(tmp);
    return result;
}

QString UnsignedChar2QString(unsigned char *str, int n)
{
    char *tmp = (char *)malloc(n+1);
    memset(tmp, 0, n+1);
    memcpy(tmp, str, n);
    QString result = QString::fromLocal8Bit(tmp);
    free(tmp);
    return result;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_NewLock_Button_clicked()
{
    this->hide();
    newlock *p_newlock = new newlock;
    connect(p_newlock, SIGNAL(back_signal()), this, SLOT(reshow()));
    p_newlock->show();
}

void MainWindow::reshow()
{
    this->show();
}

void MainWindow::on_View_Button_clicked()
{
    this->hide();
    enterpassword *p_enterpassword = new enterpassword;
    connect(p_enterpassword, SIGNAL(back_main_signal()), this, SLOT(reshow()));
    p_enterpassword->show();
}
